# Maintainer: Filipe Laíns (FFY00) <lains@archlinux.org>
# Contributor: Daniel Kozak (kozzi) <kozzi11@gmail.com>
# Contributor: Mihails Strasuns <public@dicebot.lv>
# Contributor: Moritz Maxeiner <moritz@ucworks.org>
# Contributor: Jerome Berger <jeberger@free.fr>
# Contributor: Jesus Alvarez <jeezusjr@gmail.com>
# Contributor: Allan McRae <allan@archlinux.org>
# Contributor: Elijah Stone <elronnd@elronnd.net>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - --disable-libmpx
#  - specified build host
#  - patch for AArch64

pkgbase=gdc
pkgname=(gdc libgphobos)
pkgver=8.2.1+2.068.2
#_branch=gdc-8-stable
_islver=0.20 # Change here
_gccver=8-20180831 # Change here
_gdc_commit=76136b64ad3a5b8b38992c56f7f70ec700702e76 # Change here
_gdmd_commit=0b374bfb3b0df7d0ad76de95e9dd0d5ff7ea07fa # Change here
_d_ver=''
pkgrel=2.1
arch=('x86_64')
license=('GPL3')
url="https://github.com/D-Programming-GDC/GDC"
pkgdesc="GCC based D compiler"
groups=('dlang')
makedepends=('git')
source=("ftp://gcc.gnu.org/pub/gcc/snapshots/$_gccver/gcc-$_gccver.tar.xz"
        "http://isl.gforge.inria.fr/isl-$_islver.tar.bz2"
        "gdc::git+https://github.com/D-Programming-GDC/GDC.git#commit=$_gdc_commit"
        "git+https://github.com/D-Programming-GDC/GDMD.git#commit=$_gdmd_commit"
        'paths.diff'
        '0001-AArch64-fix.patch')
sha512sums=('56f00dfb37611183a446d80f3c56c91fc4e5287801eaa5871ba6695b19654ecd424d4f9346f03ab1d30017e81fb5646beecd6cb1c63b88767ab2b3f8181fef2a'
            'afe2e159b74646a26449268637403d271f9e3f6410d8cc1c9cffca41370c4357b165dea844db0c2a654591f954e54710dda650c8088abd4711406aa6302da950'
            'SKIP'
            'SKIP'
            '841504e9dffe718f7e5a5fbbf03299f2b51acd783d47f99894aa5d411abcc56aedfffd4b16595e3a9446f2206f9eb29cb01e235e82c211796cd24dc23c02b578'
            '71bd4fa334d1c927d6f4266fa25b7bb55ad7f36fa75a260d19ff677aa66a1e05318d5f6a40b3c4e70003a6f51704236bdbc63155dbead90654684e10b544ae75')

pkgver() {
  if [ -f gdc/gcc/d/verstr.h ]; then
    _d_ver="+$(cat gdc/gcc/d/verstr.h | sed 's|\"||g')"
  elif [ -f gdc/gcc/d/VERSION ]; then
    _d_ver="+$(cat gdc/gcc/d/VERSION | sed 's|\"||g')"
  fi

  echo "$(cat gcc/gcc/BASE-VER | sed -e 's|gcc-||' -e 's|-.*||')$_d_ver"
}

prepare() {
  # Setup paths
  ln -sf "$srcdir"/gcc-$_gccver "$srcdir"/gcc
  ln -sf "$srcdir"/isl-$_islver "$srcdir"/gcc/isl

  # Setup gcc
  cd "$srcdir"/gcc

  sed -i 's|\./fixinc\.sh|-c true|' gcc/Makefile.in # Do not run fixincludes
  sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" {libiberty,gcc}/configure # hack! - some configure tests for header files using "$CPP $CPPFLAGS"

  # Seup gdc
  cd "$srcdir"/gdc

  git apply "$srcdir"/paths.diff
  git apply "$srcdir"/0001-AArch64-fix.patch
  ./setup-gcc.sh ../gcc

  mkdir "$srcdir"/gcc-build
}

build() {
  cd gcc-build

  # ALARM: Specify build host types
  [[ $CARCH == "arm" ]] && CONFIGFLAG="--host=armv5tel-unknown-linux-gnueabi --build=armv5tel-unknown-linux-gnueabi --with-arch=armv5te --with-float=soft"
  [[ $CARCH == "armv6h" ]] && CONFIGFLAG="--host=armv6l-unknown-linux-gnueabihf --build=armv6l-unknown-linux-gnueabihf --with-arch=armv6 --with-float=hard --with-fpu=vfp"
  [[ $CARCH == "armv7h" ]] && CONFIGFLAG="--host=armv7l-unknown-linux-gnueabihf --build=armv7l-unknown-linux-gnueabihf --with-arch=armv7-a --with-float=hard --with-fpu=vfpv3-d16"
  [[ $CARCH == "aarch64" ]] && CONFIGFLAG="--host=aarch64-unknown-linux-gnu --build=aarch64-unknown-linux-gnu --with-arch=armv8-a"

  # using -pipe causes spurious test-suite failures
  # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48565
  export CFLAGS="${CFLAGS/-pipe/} -O2"
  export CXXFLAGS="${CXXFLAGS/-pipe/} -O2"

  "$srcdir"/gcc/configure --prefix=/usr \
                          --libdir=/usr/lib \
                          --libexecdir=/usr/lib \
                          --mandir=/usr/share/man \
                          --infodir=/usr/share/info \
                          --enable-languages=c++,d,lto \
                          --enable-checking=release \
                          --enable-link-mutex \
                          --disable-libgomp \
                          --disable-libmudflap \
                          --disable-libquadmath \
                          --disable-multilib \
                          --enable-shared \
                          --enable-static \
                          --enable-threads=posix \
                          --disable-libmpx \
                          --with-system-zlib \
                          --with-isl \
                          --disable-libunwind-exceptions \
                          --enable-clocale=gnu \
                          --disable-libstdcxx-pch \
                          --enable-gnu-unique-object \
                          --enable-linker-build-id \
                          --enable-lto \
                          --enable-plugin \
                          --enable-install-libiberty \
                          --with-linker-hash-style=gnu \
                          --enable-gnu-indirect-function \
                          --disable-multilib \
                          --disable-werror \
                          --enable-default-pie \
                          --enable-default-ssp \
                          --with-bugurl=https://bugzilla.gdcproject.org/ \
                          --with-pkgversion="GDC ${pkgver%+*} based on D v${pkgver#*+} built with ISL $_islver for Arch Linux" \
                          gdc_include_dir=/usr/include/dlang/gdc $CONFIGFLAG

  make
}

#check() {
#  cd gcc-build
#
#  make check-d
#  grep -v "^PASS" gcc/testsuite/gdc*/gdc.sum ||:
#
#  cd x86_64-pc-linux-gnu/libphobos/libdruntime
#
#  make unittest
#  ./unittest
#}

package_gdc() {
  depends=('gcc' 'perl' 'binutils' 'libgphobos')
  provides=("d-compiler=${pkgver#*+}")
  pkgdesc="Compiler for D programming language which uses gcc backend"

  # Binaries
  install -Dm 755 gcc-build/gcc/gdc "$pkgdir"/usr/bin/gdc
  install -Dm 755 gcc-build/gcc/cc1d "$pkgdir"/usr/lib/gcc/$CHOST/${pkgver%+*}/cc1d
  install -Dm 755 GDMD/dmd-script "$pkgdir"/usr/bin/gdmd

  # Doc
  install -Dm 644 "$srcdir"/GDMD/dmd-script.1 "$pkgdir"/usr/share/man/man1/gdmd.1
}

package_libgphobos() {
  pkgdesc="Standard library for D programming language, GDC port"
  provides=('d-runtime' 'd-stdlib')
  options=('staticlibs')

  cd "$srcdir"/gcc-build
  make -C $CHOST/libphobos DESTDIR="$pkgdir" install

  if [ -d "$pkgdir"/usr/lib64 ]; then
    mv "$pkgdir"/usr/lib64/* "$pkgdir"/usr/lib
    rmdir "$pkgdir"/usr/lib64
  fi
}

